package com.newtouch.dao.base.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.newtouch.common.Page;
import com.newtouch.dao.base.BaseDao;

public class BaseDaoImpl<T, PK extends Serializable> extends
		SqlMapClientDaoSupport implements BaseDao<T, PK> {
	public String sqlmapNamespace = "";
	T entity;
	@Autowired
    public void setSqlMapClientForAutowire(SqlMapClient sqlMapClient) {  
     
       super.setSqlMapClient(sqlMapClient);
       //sqlmapNamespace=entity.getClass().getSimpleName();
       //this.setSqlmapNamespace(entity.getClass().getSimpleName());
    }
	
	// SysUser.insert
	public int deleteModelObjectAll() {
		return getSqlMapClientTemplate().delete(sqlmapNamespace + ".deleteAll");
	}

	public void insertModelObject(T entity) {
		getSqlMapClientTemplate().insert(sqlmapNamespace + ".insert", entity);
	}

	public int updateModelObject(T entity) {
		return getSqlMapClientTemplate().update(
			sqlmapNamespace + ".update", entity);
	}

	public int deleteModelObject(PK primaryKey) {
		return getSqlMapClientTemplate().delete(
			sqlmapNamespace + ".delete", primaryKey);
	}

	@SuppressWarnings("unchecked")
	public T getModelObject(PK primaryKey) {
		Object o = getSqlMapClientTemplate().queryForObject(
			sqlmapNamespace + ".selectbyid", primaryKey);
		return (T) o;
	}

	public List<T> findAllModelObjectList() {
		@SuppressWarnings("unchecked")
		List<T> list = getSqlMapClientTemplate().queryForList(
			sqlmapNamespace + ".select");
		return list;
	}
	
	public List<T> selectModelObjectListWithSQLID(String sqlId,Map<String, String> paraMap) {
		@SuppressWarnings("unchecked")
		List<T> list = getSqlMapClientTemplate().queryForList(
			sqlId, paraMap);
		return list;
	}
	
	public List<T> selectModelObjectList(Map<String, String> paraMap) {
		@SuppressWarnings("unchecked")
		List<T> list = getSqlMapClientTemplate().queryForList(
			sqlmapNamespace + ".select", paraMap);
		return list;
	}

	public List<T> selectModelObjectListInterface(Map<String, String> paraMap) {
		@SuppressWarnings("unchecked")
		List<T> list = getSqlMapClientTemplate().queryForList(
			sqlmapNamespace + ".selectInterface", paraMap);
		return list;
	}

	public List<T> selectModelObjectListPage(Page page) {
		Integer count = (Integer) this.getSqlMapClientTemplate()
				.queryForObject(sqlmapNamespace + ".count", page.getMap());
		page.computer(count.intValue());
		@SuppressWarnings("unchecked")
		List<T> list = this.getSqlMapClientTemplate().queryForList(
			sqlmapNamespace + ".selectpage", page.getMap());
		return list;
	}

	public String getSqlmapNamespace() {
		return sqlmapNamespace;
	}

	public void setSqlmapNamespace(String sqlmapNamespace) {
		this.sqlmapNamespace = sqlmapNamespace;
	}
}
